package com.springboot.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.news.domain.dto.StoreDto;
import com.springboot.news.domain.po.FormStore;
import com.springboot.news.domain.query.StoreQuery;
import com.springboot.news.domain.vo.PageableVO;
import com.springboot.news.domain.vo.StoreVo;
import com.springboot.news.mapper.StoreMapper;
import com.springboot.news.service.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, FormStore> implements StoreService {

    @Resource
    private StoreMapper storeMapper;

    @Override
    public PageableVO<StoreVo> storeList(StoreQuery dto) {
        QueryWrapper<FormStore> queryWrapper = new QueryWrapper<FormStore>();
        queryWrapper.like("store_name", dto.getStoreName());

        return PageableVO.fromPageRaw(page(dto.toPage(), queryWrapper), data -> {
            StoreVo vo = new StoreVo();
            BeanUtils.copyProperties(data, vo);

            return vo;
        });
    }

    @Override
    public void create(StoreDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        FormStore query = new FormStore();
        BeanUtils.copyProperties(dto, query);
        save(query);
    }

    @Override
    public void delete(Integer id) {
        storeMapper.deleteById(id);
    }
    @Override
    public void update(StoreDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        FormStore query = new FormStore();
        BeanUtils.copyProperties(dto, query);
        updateById(query);
    }

    @Override
    public StoreVo detail(Integer id) {
        FormStore po = getById(id);
        StoreVo vo =  new StoreVo();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }
}
